import request from './axios'
import qs from 'qs'

export function get(config) {
  let { url, data, testUrl } = config
  if (!url.startsWith('http')) {
    url = window.SITE_CONFIG.baseUrl + url
  }
  if (testUrl) { // 测试路径
    url = testUrl
  }
  return new Promise((resolve, reject) => {
    // 如果是ie浏览器要添加个时间戳，解决浏览器缓存问题
    if (!!window.ActiveXObject || 'ActiveXObject' in window) {
      data['time'] = new Date().getTime()
    }
    request.get(url, {
      params: data,
      paramsSerializer: data => {
        return qs.stringify(data, { indices: false })
      }
    }).then(response => {
      resolve(response.data)
    }).catch(err => {
      reject(err)
    })
  })
}

export function post(config) {
  let { url, data, testUrl } = config
  if (!url.startsWith('http')) {
    url = window.SITE_CONFIG.baseUrl + url
  }
  if (testUrl) { // 测试路径
    url = testUrl
  }
  data = JSON.stringify(data)
  return new Promise((resolve, reject) => {
    request.post(url, data).then(response => {
      resolve(response.data)
    }).catch(err => {
      reject(err)
    })
  })
}
